<?php

namespace plugin\ycMassage\process;

use app\common\model\StoreApp;
use plugin\ycMassage\app\model\MassageOrders;
use plugin\ycMassage\app\model\MassageUser;
use plugin\ycMassage\app\model\MassageUserDealer;
use plugin\ycMassage\app\model\MassageUserDealerLog;
use plugin\ycMassage\app\model\MassageUserDealerStatistics;
use plugin\ycMassage\app\model\MassageUserWallet;
use plugin\ycMassage\app\model\MassageWithdrawal;
use plugin\ycMassage\utils\enum\ActionIdentity;
use plugin\ycMassage\utils\enum\OrdersState;
use think\facade\Db;

class OrderExpire
{
    public function run()
    {
        try {
            $OrderList = MassageOrders::where(['state' => OrdersState::NOT_PAY['value']])
                ->whereTime('expire_time', '<', date('Y-m-d H:i:s'))->select();
            foreach ($OrderList as $item) {
                MassageOrders::cancel($item->trade);
            }
        } catch (\Throwable $th) {
            p([$th->getMessage(), $th->getLine(), $th->getFile()], 'MassageOrderExpire');
        }
    }
    public function dealer(){
        try {
            $StoreApp = StoreApp::where(['name' => 'ycMassage', 'status' => 20])->column('id');
            foreach ($StoreApp as $key => $saas_appid) {
                $MassageUserDealerStatistics = MassageUserDealerStatistics::where(['saas_appid' => $saas_appid])->whereDay('create_time', 'yesterday')->find();
                if ($MassageUserDealerStatistics) {
                    continue;
                }
                try {
                    $MassageUserDealerStatistics = new MassageUserDealerStatistics;
                    $MassageUserDealerStatistics->saas_appid = $saas_appid;
                    $MassageUserDealerStatistics->create_time = date('Y-m-d H:i:s', strtotime('-1 day'));
                    $MassageUserDealerStatistics->OrderNum = MassageUserDealer::where(['saas_appid' => $saas_appid])->sum('order_num');
                    $MassageUserDealerStatistics->OrderPrice = MassageUserDealer::where(['saas_appid' => $saas_appid])->sum('order_price');
                    $MassageUserDealerStatistics->DealerLogNum = MassageUserDealerLog::where(['saas_appid' => $saas_appid])->count('id');
                    $MassageUserDealerStatistics->DealerPrice = MassageUserWallet::where(['saas_appid' => $saas_appid])->sum('dealer_money_sum');
                    $MassageUserDealerStatistics->WithdrawalPriceStock = MassageUserWallet::where(['saas_appid' => $saas_appid])->sum('dealer_money');
                    $MassageUserDealerStatistics->WithdrawalPrice = MassageWithdrawal::where(['saas_appid' => $saas_appid,'action'=>ActionIdentity::DEALER['value']])->sum('money');
                    $MassageUserDealerStatistics->DealerPeopleNum = MassageUserDealer::where(['saas_appid' => $saas_appid])->count('uid');
                    $MassageUserDealerStatistics->UserNum = MassageUser::where(['saas_appid' => $saas_appid])->whereNotNull('dealer_uid')->count('id');
                    $MassageUserDealerStatistics->save();
                } catch (\Throwable $th) {
                    p([$th->getMessage(), $th->getLine(), $th->getFile()], 'MassageDealerStatistics Error');
                }
            }
        } catch (\Throwable $th) {
            p([$th->getMessage(), $th->getLine(), $th->getFile()], 'MassageDealerStatistics');
        }
    }
}
